package com.example.administrator.yuejian23;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.yuejian23.Activity.RegActivity;
import com.example.administrator.yuejian23.Bean.LoginBean;
import com.example.administrator.yuejian23.Fragment.BaseFragment;
import com.example.administrator.yuejian23.Retrofit.RetrofitService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;


/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends BaseFragment {

    private EditText telView;
    private EditText passwordView;
    private Button loginButton;
    private Button registerButton;


    public LoginFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        telView = (EditText) view.findViewById(R.id.editText_tel);
        passwordView = (EditText) view.findViewById(R.id.editText_password);
        loginButton = (Button) view.findViewById(R.id.button_login);
        registerButton = (Button) view.findViewById(R.id.button_register);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Retrofit retrofit =
                        new Retrofit.Builder()
                                .baseUrl(Const.BASE_URL)
                                .addConverterFactory(GsonConverterFactory.create())
                                .build();
                RetrofitService retrofitService =
                        retrofit.create(RetrofitService.class);
                String username = telView.getText().toString();
                String password =passwordView.getText().toString();
                Call<LoginBean> loginCall = retrofitService.userLogin(username,password);

                loginCall.enqueue(new Callback<LoginBean>() {
                    @Override
                    public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                        LoginBean loginBean = response.body();
                        if(!loginBean.getId().equals("0")){
                            Toast.makeText(getActivity(), "登录成功", Toast.LENGTH_SHORT).show();
                            MainActivity activity = (MainActivity) getActivity();
                            if(activity != null){
                                activity.replaceLoginFragment();

                                String userId = loginBean.getId();
                                activity.saveUserId(userId);
                                activity.setUserId(userId);

                            }

                        }else {
                            Toast.makeText(getActivity(), "用户名或密码错误", Toast.LENGTH_SHORT).show();

                        }
                    }

                    @Override
                    public void onFailure(Call<LoginBean> call, Throwable t) {

                    }
                });

            }
        });
        registerButton.setOnClickListener(  new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), RegActivity.class);
                startActivityForResult(intent,1);

            }
        });


    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0){
            switch (requestCode){
                case RESULT_OK:
                    String username = data.getStringExtra("username");
                    telView.setText(username) ;
                    break;
                case RESULT_CANCELED:
                    break;

            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_login, container, false);
    }

}